package com.imporven.sell.controller;

import com.imporven.sell.Collect.HashMap;
import com.imporven.sell.convert.OrderForm2OrderDTOConvert;
import com.imporven.sell.dto.OrderDTO;
import com.imporven.sell.enums.ResultEnum;
import com.imporven.sell.exception.SellException;
import com.imporven.sell.form.OrderForm;
import com.imporven.sell.service.BuyerService;
import com.imporven.sell.service.OrderService;
import com.imporven.sell.util.ResultVoUtil;
import com.imporven.sell.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author imporven
 * @version 1.0.0
 * @date 2018-7-10 23:26
 * 买家订单controller
 */
@RestController
@RequestMapping("/sell/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    @PostMapping("/create")
    public ResultVo create(@Valid OrderForm orderForm,
                           BindingResult bindingResult){
        //1.校验参数
        if(bindingResult.hasErrors()){
            log.error("【创建订单】,参数不正确,orderForm={},errorMassage={}",orderForm,bindingResult.getAllErrors());
            throw new SellException(ResultEnum.PARAMETER_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        //2.orderForm -> DTO
        OrderDTO orderDto = OrderForm2OrderDTOConvert.convert(orderForm);
        if(CollectionUtils.isEmpty(orderDto.getOrderDetailList())){
            log.error("【创建订单】,购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        //3.订单入库
        OrderDTO createResult = orderService.create(orderDto);

        //4.组装数据返回给前台
        Map<String,String> data = new HashMap<String,String>(){{
                put("orderId",createResult.getOrderId());
        }};

        return ResultVoUtil.success(data);
    }

    @GetMapping("/list")
    public ResultVo<OrderDTO> orderList(@RequestParam(name="openid") String openid,
                                        @RequestParam(name="page",defaultValue = "0") Integer page,
                                        @RequestParam(name="size",defaultValue = "10") Integer size){

        if(StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAMETER_ERROR.getCode(),"openid不能为空");
        }

        PageRequest pageRequest = PageRequest.of(page,size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid,pageRequest);

        return ResultVoUtil.success(orderDTOPage.getContent());
    }

    @GetMapping("/detail")
    public ResultVo<OrderDTO> orderDetail(@RequestParam(name="openid") String openid,
                                          @RequestParam(name="orderId") String orderId){

        if(StringUtils.isEmpty(openid)){
            log.error("【查找订单明细】openid参数为空");
            throw new SellException(ResultEnum.PARAMETER_ERROR.getCode(),"openid不能为空");
        }
        if(StringUtils.isEmpty(orderId)){
            log.error("【查找订单明细】orderId参数为空");
            throw new SellException(ResultEnum.PARAMETER_ERROR.getCode(),"orderId不能为空");
        }

        OrderDTO orderDTO = buyerService.findOrderOne(openid,orderId);
        return ResultVoUtil.success(orderDTO);
    }

}
